<?php

namespace App\Admin\Controllers\Share;

use App\model\broker\BrokerProject;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class BrokerProjectController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('推广项目')
            ->description('推广项目')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('推广项目')
            ->description('推广项目')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑推广项目')
            ->description('编辑推广项目')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新建推广项目')
            ->description('新建推广项目')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new BrokerProject);

        $grid->id('Id');
        $grid->title('名称');
      //  $grid->desc('Desc');
     //   $grid->effective_first_reward('Effective first reward');
    //    $grid->effective_share_reward('Effective share reward');
    //    $grid->follow_first_reward('Follow first reward');
    //    $grid->follow_share_reward('Follow share reward');
    //    $grid->transaction_first_reward('Transaction first reward');
    //    $grid->transaction_share_reward('Transaction share reward');
     //   $grid->second_reward('Second reward');
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');
        $grid->column('yulan', '预览')->display(function ($vlaue) {
            return "<a href='" . config('admin.app_outurl') . "/distribution/{$this->id}' target='_blank'> 查看</a>";
        });
        //筛选功能
        $grid->filter(function (Grid\Filter $filter) {
            $filter->disableIdFilter();
            $filter->like('title', '标题');
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(BrokerProject::findOrFail($id));

        $show->id('Id');
        $show->title('项目标题');
        $show->type('产品类型');
        $show->produce_id('产品名称');
        $show->total('总奖金池');
        $show->last('剩余奖金池');
        $show->desc('项目内容');
        $show->effective_first_reward('有效推广奖励');
        $show->effective_share_reward('有效推广朋友圈奖励');
        $show->follow_first_reward('继续跟进推广奖励');
        $show->follow_share_reward('继续跟进分享朋友圈奖励');
        $show->transaction_first_reward('成交推广奖励');
        $show->transaction_share_reward('成交分享朋友圈奖励');
        $show->second_reward('上级推广分成');
        $show->created_at('创建时间');
        $show->updated_at('更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new BrokerProject);
        $form->tab('基本信息',function(Form $form){
            $form->text('title', '名称');
            $type = [
                0 => '厂房',
                1 => '土地',
                2 => '写字楼',
            ];
            $form->select('type', '产品类型')->options($type)->load('produce_id', '/admin/api/type');
            $form->select('produce_id', '推广产品');
            $form->ckeditor('desc', '微信分享描述');
        })->tab('分享规则',function(Form $form){
            $form->currency('total', '总奖金池')->symbol('万元');
            $form->currency('effective_first_reward', '有效推广奖励')->symbol('元');
            $form->currency('effective_share_reward', '有效推广朋友圈奖励')->symbol('元');
            $form->currency('follow_first_reward', '继续跟进推广奖励')->symbol('元');
            $form->currency('follow_share_reward', '继续跟进分享朋友圈奖励')->symbol('元');
            $form->currency('transaction_first_reward', '成交推广奖励')->symbol('元');
            $form->currency('transaction_share_reward', '成交分享朋友圈奖励')->symbol('元');
            $form->currency('second_reward', '上级推广分成')->symbol('%');
        });


        return $form;
    }
}
